package com.toman.Adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.R;
import com.toman.bean.RankBean;

import java.text.DecimalFormat;
import java.util.List;

import com.squareup.picasso.Picasso;

/**
 * Created by 邹苏隆 on 2018/4/3.
 * 描述：排行榜适配器
 */

public class RankAdapter extends AdapterBase<RankBean, RankHolder> {
    private LayoutInflater inflater;
    private Context mContext;

    public RankAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;

    }

    @Override
    protected void handlerData(List<RankBean> mList2, int position, RankHolder itemView) {
        RankBean bean = mList2.get(position);
        itemView.tv_name.setText(bean.getName());
        int num = position + 1;
        double d = Double.parseDouble(bean.getStatus());
        itemView.tv_number.setText("" + num);
        DecimalFormat format = new DecimalFormat("0.00");
        String s = format.format(d); // 56.79%
        itemView.tv_status.setText(s);

        if (num == 1) {
            itemView.iv_show.setImageResource(R.mipmap.rankgoldcrown);
            itemView.tv_status.setTextColor(mContext.getResources().getColor(R.color.s2));
            itemView.iv_show.setVisibility(View.VISIBLE);
        } else if (num == 2) {
            itemView.iv_show.setImageResource(R.mipmap.rankslivercrown);
            itemView.tv_status.setTextColor(mContext.getResources().getColor(R.color.s2));
            itemView.iv_show.setVisibility(View.VISIBLE);

        } else if (num == 3) {
            itemView.iv_show.setImageResource(R.mipmap.rankcoppercrown);
            itemView.tv_status.setTextColor(mContext.getResources().getColor(R.color.s2));
            itemView.iv_show.setVisibility(View.VISIBLE);

        } else {
            itemView.tv_status.setTextColor(mContext.getResources().getColor(R.color.green));
            itemView.iv_show.setVisibility(View.INVISIBLE);

        }
        if (null == bean.getHead() || bean.getHead().equals("")) {
            Picasso.with(mContext).load(R.mipmap.avatar_default_login).into(itemView.iv_head);
        } else {
            Picasso.with(mContext).load(bean.getHead()).error(R.mipmap.avatar_default_login).into(itemView.iv_head);

        }
    }


    @Override
    protected RankHolder getItemViewHolder() {
        return new RankHolder();
    }

    @Override
    protected View initConvertView(View convertView, RankHolder v) {
        convertView = inflater.inflate(R.layout.item_rank, null);
        v.tv_number = (TextView) convertView
                .findViewById(R.id.tv_number);
        v.iv_head = (ImageView) convertView
                .findViewById(R.id.iv_head);
        v.tv_name = (TextView) convertView
                .findViewById(R.id.tv_name);
        v.iv_show = (ImageView) convertView
                .findViewById(R.id.iv_show);
        v.tv_status = (TextView) convertView
                .findViewById(R.id.tv_status);
        return convertView;
    }

    @Override
    protected void onReachBottom() {

    }
}

class RankHolder {
    public ImageView iv_head;
    public TextView tv_number;
    public TextView tv_name;
    public TextView tv_status;
    public ImageView iv_show;

}

